package com.example.zelonghu.test_call_in_out2;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
import DateUtils.dateutils;
public class PhoneBroadcastReceiver extends BroadcastReceiver {
    private TelephonyManager tm;
    private static long starttime = 0;
    private static  long endtime = 0;
    @Override
    public void onReceive(Context context, Intent intent) {
        //判断是否为手机行为状态
        if("android.intent.action.PHONE_STATE".equals(intent.getAction())){
            //获取电脑管理者
            tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            int state=tm.getCallState();
            //获取电话号码
            String phone=intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            switch (state){
                case TelephonyManager.CALL_STATE_RINGING:
                    Log.i("test",phone+"来电");
                     starttime = System.currentTimeMillis();

                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    Log.i("test",phone+"正在通话中");
                    break;
                case TelephonyManager.CALL_STATE_IDLE:

                    endtime = System.currentTimeMillis();
                    Log.i("test",dateutils.stampToDate(endtime +"","HH:mm:ss"));

                    long dur = endtime-starttime;
                    String re = dateutils.stampToDate(dur,"mm:ss");
                    Log.i("test","通话结束了 时间:" + re);
                    break;
            }
        }

    }
/*@Override
public void onReceive(Context context, Intent intent) {
String number = getResultData();
if("5556".equals(number)){
setResultData(null);//挂断
}else{
number = "12593"+ number; //其他，则加区号
setResultData(number);
}
}*/
}